package cn.wolfcode.shop.member.web.controller.advice;

import cn.wolfcode.shop.common.exception.BusinessException;
import cn.wolfcode.shop.common.util.CodeMsg;
import cn.wolfcode.shop.common.util.Result;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class MemberServerExceptionAdvice {

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result hanlderBusinessException(BusinessException businessException){

        CodeMsg codeMsg = businessException.getCodeMsg();

        return Result.error(codeMsg);
    }

    @ExceptionHandler(BindException.class)
    @ResponseBody
    public Result hanlderBindException(BindException bindexception){

        String message = bindexception.getAllErrors().get(0).getDefaultMessage();

        return Result.error(new CodeMsg(500,message));
    }
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result hanlderBusinessException(RuntimeException runtimeException){

        String message = runtimeException.getMessage();

        return Result.defaultError();
    }

}